import java.lang.reflect.Array;
import java.util.ArrayList;

public class 求水仙花数 {
    public static void main(String[] args) {
        System.out.println("方法1："+method1());
        System.out.println("方法2："+method2());
    }

    static ArrayList<Integer> method1(){
        int begin_num = 100;
        ArrayList<Integer> water_nums = new ArrayList<>();
        for(;begin_num<=999;begin_num++){
            String t_str = ""+begin_num;
            int t0 = Character.getNumericValue((t_str.charAt(0)));//先将字符串按下标取字符，再将字符转为数值
            int t1 = Character.getNumericValue((t_str.charAt(1)));
            int t2 = Character.getNumericValue((t_str.charAt(2)));
            if(nSquare(t0,3)+nSquare(t1,3)+nSquare(t2,3)==begin_num){
                water_nums.add(begin_num);
                //System.out.println(begin_num);
            }
        }
        return water_nums;
    }

    static int nSquare(int num,int n){
        int raw_num = num;//原始乘数
        for (int i=1;i<n;i++){
            num*=raw_num;
        }
        return num;
    }

    static ArrayList<Integer> method2(){
        ArrayList<Integer> waterNums = new ArrayList<>();
        for(int i=100;i<=999;i++){
            int numIndex1 = i/100;//int会自动去掉小数点，举例123/100=1.23->1
            int numIndex2 = (i%100)/10;//123%100=23,23/10=2.3->2//如果不清楚算术优先级，还是加上括号保险
            int numIndex3 = i%10;//123%10=3
            if(nSquare(numIndex1,3)+nSquare(numIndex2,3)+nSquare(numIndex3,3)==i){
                waterNums.add(i);
            }
        }
        return waterNums;
    }
    //方法1用的代码方法取值，方法2用的数学方法取值
}
